<?php
class My_API_ss32_Users
{
    private $_config;
    
    public function __construct($gameId = null)
    {
        if(!isset($gameId) || !file_exists(CONFIG_PATH2 . "/$gameId.ini")){
            return false;
        }
        
        $config = new Zend_Config_Ini(CONFIG_PATH2 . "/$gameId.ini",APPLICATION_ENV);
        $this->_config = $config;
    }
    
    public function getUsersByServerId($user){
        date_default_timezone_set('Asia/Ho_Chi_Minh');
        header('Content-Type: text/html; charset=utf-8');
        set_time_limit(0);
        
        $url=$this->_config->getUserInfo->url;
        $clientKey = '2621fafbb8b403cd1633de2e6a9cb844';
        $user_id =$user ;
       // '806893922674448,766986143352513,264960210373948,b100001628316202,b100001649019985,b100000655315330,b100000677058976'
        $postData = array(
              'user_id' => $user_id,
              'signature' => md5($clientKey)
            );
            
        $fields = '';
        foreach($postData as $key => $value) { 
            $fields .= $key . '=' . $value . '&'; 
        }
        rtrim($fields, '&');
        
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
        $result = curl_exec($curl);
        curl_close($curl);
        
        $result = json_decode($result, True);
        if(isset($result->error_code)){
            echo 'Error: '.$result->error_message;
            exit;
        }
      
        if($result['status'] == 1){
            $dataUser=array();
            if($result['result']){
                $result['status']=(current($result['result']));
                $dataUser['roleName']= $result['status']['displayName'];
                $dataUser['info']=$result['status']['info'];
                return $dataUser;
            }
            else{
                continue;
            }
        }
    }
    
    public function getUsernameByRolename($listRoleName){
        $infoUsers = array();
        date_default_timezone_set('Asia/Ho_Chi_Minh');
        header('Content-Type: text/html; charset=utf-8');
        set_time_limit(0);
        $url=$this->_config->getUsernameInfo->url;
        $clientKey = '2621fafbb8b403cd1633de2e6a9cb844';
        $displayName = "{$listRoleName}";
        //'Vu Duc,Anh Pham'
        $postData = array(
              'displayName' => $displayName,
              'signature' => md5($clientKey)
            );
        $fields = '';
        foreach($postData as $key => $value) { 
            $fields .= $key . '=' . $value . '&'; 
        }
        rtrim($fields, '&');
        
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
        $result = curl_exec($curl);
        curl_close($curl);
        $result = json_decode($result, True);
       
        if(isset($result['status']) && $result['status'] == 1){   
            $result['result']=current(current($result));
            if($result['result']){
                $infoUsers['username']=$result['result']['user_id'];
                $infoUsers['info']=$result['result']['info'];
                return $infoUsers;
            }  
        }
        else
        {
            continue;
        }
       
    }
}